/*
 * Project Name spring-boot-tools
 * File Name MybatisPlusConfiguration
 * Package Name com.huxiaosu.tools.mybatis.configuration
 * Create Time 2024/7/15
 * Create by name：liujie
 */
package com.huxiaosu.tools.mybatis.extend.configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.huxiaosu.tools.mybatis.extend.handlers.MapperExtend;
import com.huxiaosu.tools.mybatis.extend.injector.BatchSaveInjector;
import com.huxiaosu.tools.mybatis.extend.tenant.TenantInterceptor;
import com.huxiaosu.tools.mybatis.extend.tenant.TenantProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

/**
 * Description
 *
 * @author liujie
 * @date 2024/7/15 15:28
 */
@EnableConfigurationProperties({TenantProperties.class })
public class MybatisPlusConfiguration {
    @Autowired
    private TenantProperties tenantProperties;

    @Bean
    @Primary
    public BatchSaveInjector cloudSqlInjector() {
        return new BatchSaveInjector();
    }

    @Bean
    public MapperExtend mapperExtend() {
        return new MapperExtend();
    }
    @Bean
    @ConditionalOnProperty(value = "tools.tenant.enable")
    public TenantInterceptor tenantInterceptor() {
        return new TenantInterceptor(tenantProperties);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if (tenantProperties.isEnable()) {
            interceptor.addInnerInterceptor(new TenantInterceptor(tenantProperties));
        }
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}